import 'package:dio/dio.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';

class BaseDio {
  /// 请求超时时间
  final int _connectTimeout = 60000;

  /// 响应超时时间
  final int _receiveTimeout = 60000;

  /// api请求地址
  final String _baseUrl = "https://itunes.apple.com/hk";

  // /// 是否开启调试
  // final bool _isDebug = false;

  /// 是否打印请求日志
  final bool _isLogsEnabled = false;

  BaseDio._();

  static final BaseDio _instance = BaseDio._();

  static BaseDio getInstance() => _instance;

  Dio getDio() {
    final Dio dio = Dio();
    dio.options = BaseOptions(
      baseUrl: _baseUrl,
      receiveTimeout: _connectTimeout,
      connectTimeout: _receiveTimeout,
      contentType: Headers.jsonContentType,
      responseType: ResponseType.json,
      validateStatus: (status) => status! >= 200 && status < 400,
      headers: {
        Headers.contentTypeHeader: Headers.jsonContentType,
      },
    ); // 设置超时时间等 ...
    // dio.interceptors.add(HeaderInterceptor()); // 添加拦截器，如 token之类，需要全局使用的参数

    if (_isLogsEnabled) {
      dio.interceptors.add(PrettyDioLogger(
        // 添加日志格式化工具类
        requestHeader: true,
        requestBody: true,
        responseBody: true,
        responseHeader: false,
        compact: false,
      ));
    }

    return dio;
  }
}
